ZONES The leftmost part of the screen is called the first zone. To the right of that zone lies the second zone. If your computer's fancy, it also has a third, fourth, and fifth zone; and each zone 16 characters wide, so the entire screen is 80 characters wide. (If your computer's not so fancy, it might have fewer zones, or the zones might be narrower; and the screen might be less than 80 characters wide. To find out about your computer's peculiarities, read the ``Versions of BASIC'' appendix.) A comma makes the computer jump to a new zone. Here's an example: PRINT "SIN","KING" The computer will print SIN and KING on the same line; but because of the comma before KING, the computer will print KING in the second zone, like this: SIN KING first zone second zone third zone fourth zone fifth zone To turn that example into a program, put a number in front of the line, like this: 10 PRINT "SIN","KING" When you type RUN, the computer will print: SIN KING This program does the same thing: 10 PRINT "SIN", 20 PRINT "KING" Line 10 makes the computer print SIN and then jump to the next zone. Line 20 makes the computer print KING. The computer will print: SIN KING This example's silly: PRINT "LOVE","CRIES","OUT" The computer will print LOVE in the first zone, CRIES in the second zone, and OUT in the third zone, like this: LOVE CRIES OUT This example's even sillier: PRINT "LOVE","CRIES","OUT","TO","ME","AT","NIGHT" The computer will print LOVE in the first zone, CRIES in the second, OUT in the third, TO in the fourth, ME in the fifth, and the remaining words below, like this: LOVE CRIES OUT TO ME AT NIGHT This example tells a bad joke: PRINT "I THINK YOU ARE UGLY","I'M JOKING" The computer will print I THINK YOU ARE UGLY, then jump to a new zone, then print I'M JOKING, like this: I THINK YOU ARE UGLY I'M JOKING first zone second zone third zone fourth zone fifth zone When you combine commas with semicolons, you can get weird results: PRINT "EAT","ME";"AT";"BALLS","NO";"W" That line contains commas and semicolons. A comma makes the computer jump to a new zone, but a semicolon does not make the computer jump. The computer will print EAT, then jump to a new zone, then print ME and AT and BALLS, then jump to a new zone, then print NO and W. Altogether, the computer will print: EAT MEATBALLS NOW Skipping a zone You can make the computer skip over a zone: PRINT "JOE"," ","LOVES SUE" The computer will print JOE in the first zone, a blank space in the second zone, and LOVES SUE in the third zone, like this: JOE LOVES SUE first zone second zone third zone fourth zone fifth zone You can type that example even more briefly, like this: PRINT "JOE",,"LOVES SUE" Loops This program makes the computer greet you: 10 PRINT "HI", 20 GO TO 10 The computer will print HI many times. Each time will be in a new zone, like this: HI HI HI HI HI HI HI HI HI HI HI HI HI HI HI etc. Tables This program prints a list of words and their opposites: 10 PRINT "GOOD","BAD" 20 PRINT "BLACK","WHITE" 30 PRINT "GRANDPARENT","GRANDCHILD" 40 PRINT "HE","SHE" Line 10 makes the computer print GOOD, then jump to the next zone, then print BAD. Altogether, the computer will print: GOOD BAD BLACK WHITE GRANDPARENT GRANDCHILD HE SHE The first zone contains a column of words; the second zone contains the opposites. Altogether, the computer's printing looks like a table. So whenever you want to make a table, use zones, by putting commas in your program. Let's make the computer print this table: NUMBER SQUARE 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100 Here's the program: 10 PRINT "NUMBER","SQUARE" 20 FOR I = 3 TO 10 30 PRINT I,I*I 40 NEXT Line 10 prints the word NUMBER at the top of the first column, and the word SQUARE at the top of the second. Line 20 says I goes from 3 to 10; to begin, I is 3. Line 30 makes the computer print: 3 9 Line 40 makes the computer do the same thing for the next I, and for the next I, and for the next; so the computer prints the whole table. TAB When the computer puts a line of information on your screen, the leftmost character in the line is said to be at position 1. The second character in the line is said to be at position 2. This command makes the computer skip to position 6 and then print ``HOT'': PRINT TAB(6)"HOT" The computer will print: HOT 12345678 Here's a fancier example: PRINT TAB(6)"HOT";TAB(13)"BUNS" The computer will skip to the 6th position, then print ``HOT'', then skip to the 13th position, then print ``BUNS'': HOT BUNS 12345678 13 Diagonal This program prints a diagonal line: 10 FOR I = 1 TO 12 20 PRINT TAB(I)"*" 30 NEXT Line 10 says to do the loop twelve times, so the computer does line 20 repeatedly. The first time the computer does line 20, the I is 1, so the computer prints an asterisk at position 1: * The next time, the I is 2, so the computer skips to position 2 and prints an asterisk: * The next time, the I is 3, so the computer skips to position 3 and prints an asterisk: * Altogether, the program makes the computer print this picture: * * * * * * * * * * * * Calendar Let's make the computer print a calendar for the whole year. We must begin by telling the computer how many days are in each month: Month's name Month's length January 31 February 28 or 29 March 31 April 30 May 31 June 30 July 31 August 31 September 30 October 31 November 30 December 31 That list becomes our data: 10 DATA JANUARY,31,FEBRUARY,28,MARCH,31,APRIL,30 20 DATA MAY,31,JUNE,30,JULY,31,AUGUST,31 30 DATA SEPTEMBER,30,OCTOBER,31,NOVEMBER,30,DECEMBER,31 (For a leap year, change line 10.) To make the computer look at that data, tell the computer to READ: 40 READ N$,L That makes the computer read a month's Name and Length. To read all the data, the computer should do line 40 twelve times. So put line 40 inside a loop, like this: English BASIC Here are the months 10 DATA JANUARY,31,FEBRUARY,28,MARCH,31,APRIL,30 and their lengths. 20 DATA MAY,31,JUNE,30,JULY,31,AUGUST,31 30 DATA SEPTEMBER,30,OCTOBER,31,NOVEMBER,30,DECEMBER,31 For each month, 39 FOR M = 1 TO 12 read its Name & Length, 40 READ N$,L print its Name, 50 PRINT N$ print all its days, 60 FOR D = 1 TO L 70 PRINT D; 80 NEXT and press the ENTER key 90 PRINT twice at end of the month. 91 PRINT 100 NEXT When you run that program, the computer prints a calendar beginning like this: JANUARY 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 FEBRUARY 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 MARCH 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Pretty weeks Although that program makes the computer print the right numbers for each month, it prints the numbers in the wrong places. Let's make the computer print at most 7 numbers in each row, so each is a week. To print the numbers in the right places, use TAB: 70 PRINT TAB(T)D; Line 70 will make the computer print each day in the right position . . . if we define T correctly. But how should we define T? For Sunday, let's make T be 2, so that Sunday begins at position 2. For Monday, let's make T be 6, so that Monday begins at position 6. For Tuesday, let's make T be 10; for Wednesday, 14; Thursday, 18; Friday, 22; and Saturday, 26. So whenever a day's been printed, T should normally increase by 4 for the next day: 71 T=T+4 Saturday's the last day of the week; after Saturday, we must begin a new week. So if T has passed Saturday (which is 26), we want T to become 2 (for Sunday); and if there are more days left in the month, we want the computer to press the ENTER key (to start a new week): 72 IF T>26 THEN T=2: IF D26 THEN T=2: IF D26 THEN T=2: IF D"I DO" THEN BEEP: PRINT "TRY AGAIN!": GO TO 10 30 INPUT "DO YOU TAKE THIS MAN TO BE YOUR LAWFUL WEDDED HUSBAND";A$ 40 IF A$<>"I DO" THEN BEEP: PRINT "TRY AGAIN!": GO TO 30 50 PRINT "I NOW PRONOUNCE YOU HUSBAND AND WIFE." Line 10 makes the computer ask the groom, ``DO YOU TAKE THIS WOMAN TO BE YOUR LAWFUL WEDDED WIFE?'' If the groom doesn't say ``I DO'', line 20 makes the computer beep, say ``TRY AGAIN!'', and repeat the question. Lines 30 and 40 do the same thing to the bride. Line 50 congratulates the couple for having answered correctly. SOUND If you type ___ SOUND 440,18.2 the typical computer will produce a sound. In that command, the 440 is the frequency (``pitch''), measured in hertz (cycles per second); so the sound will be a musical note whose pitch is 440 hertz. (That note happens to be ``the A above middle C''). If you replace the 440 by a higher number, the sound will have a higher pitch. When you were a baby, you could probably hear up to 20000. As you get older, your hearing gets worse, and you can't hear such high notes. Today, the highest sound you can hear is probably somewhere around 14000. To find out, give yourself a hearing test, by running this program: 10 INPUT "WHAT PITCH WOULD YOU LIKE ME TO PLAY";P 20 SOUND P,18.2 30 GO TO 10 When you run that program, begin by inputting a low pitch (such as 200). Then input a higher number, then an even higher number, until you finally pick a number so high you can't hear it. (When trying that test, put your ear close to the computer's speaker, which is in the computer's front left corner.) When you've picked a number too high for you to hear, try a slightly lower number. Keep trying different numbers, until you find the highest number you can hear. Have a contest with your friends: find out which of your friends can hear best. If you run that program every year, you'll see that your hearing gets gradually worse. For example, when I was 36 years old, the highest pitch I could hear was about 14500, but I can't hear that high anymore. How about you? In those examples, the 18.2 makes the computer produce the sound for 1 second. If you want the sound to last longer ___ so that it lasts 2 seconds ___ replace the 18.2 by 18.2*2. For 10 seconds, say 18.2*10. (That's because the computer's metronome beats 18.2 times per second.) PLAY If you type ___ PLAY "CDG#B-A" the typical computer will play the note C, then D, then G sharp, then B flat, then A. Octave The computer can play in seven octaves, numbered from 0 to 6. Octave 0 consists of very bass notes; octave 6 consists of very high-pitched notes. In each octave, the lowest note is a C: the notes in an octave are C, C#, D, D#, E, F, F#, G, G#, A, A#, and B. ``Middle C'' is at the beginning of octave 2. Normally, the computer plays in octave 4. To make the computer switch to octave 3, type the letter ``O'' followed by a 3, like this: PLAY "O3" After giving that command, anything else you PLAY will be in octave 3, until you change octaves again. Length Besides playing with pitches, you can also play with rhythms (``lengths'' of the notes). Normally each note is a ``quarter note''. To make the computer switch to eighth notes (which are faster), type this: PLAY "L8" Besides using L8 for eighth notes, you can use L16 for sixteenth notes (which are even faster), L32 for thirty-second notes (which are super-fast), and L64 for sixty-fourth notes (which are super-super-fast). For long notes, you can use L2 (which gives a half note) or L1 (which gives a whole note). You can use any length from L1 to L64. You can even use in-between lengths, such as L7 or L23 (though such rhythms are hard to stamp your foot to). Dots If you put a period after a note, the computer will multiply the note's length by 1«. For example, suppose you say: PLAY "L8CE.D" The C will be an 8th note, E will be 1« times as long as an 8th note, and D will be an 8th note. Musicians call that E a dotted eighth note. If you put two periods after a note (like this: E..), the computer will multiply the note's length by 13/4. Musicians say the note is double dotted. If you put three periods after a note (like this: E...), the computer will multiply the note's length by 17/8. Pause To make the computer pause (``rest'') for an eighth note, put a P8 into the music string. Tempo Normally, the computer plays 120 quarter notes per minute; but you can change that tempo. To switch to 150 quarter notes per minute, say: PLAY "T150" You can switch to any tempo from 32 to 255. The 32 is very slow; 255 is very fast. In musical terms, 40=larghissimo, 50=largo, 63=larghetto, 65=grave, 68=lento, 71=adagio, 76=andantino, 92=andante, 114=moderato, 120=allegretto, 144=allegro, 168=vivace, 188=presto, and 208=prestissimo. Combine them You can combine all those musical commands into a single PLAY statement. For example, to set the tempo to 150, the octave to 3, the length to 8 (which means an eighth note), and then play C and D, and then change the length to 4 and play E, type this: PLAY "T150O3L8CDL4E" PRINT USING Suppose you want to add $12.47 to $1.03. The correct answer is $13.50. This almost works: PRINT 12.47+1.03 It makes the computer print: 13.5 But instead of 13.5, we should try to make the computer print 13.50. This command forces the computer to print 13.50: PRINT USING "##.##"; 12.47+1.03 The ``##.##'' is called the picture or image or format: it says to print two characters, then a decimal point, then two digits. The computer will print: 13.50 This command puts that answer into a sentence: PRINT USING "YOU SPENT ##.## AT OUR STORE"; 12.47+1.03 The computer will print: YOU SPENT 13.50 AT OUR STORE Rounding This program makes the computer divide 300 by 7 but round the answer to two decimal places: PRINT USING "##.##"; 300/7 When the computer divides 300 by 7, it gets 42.8571, but the format rounds the answer to 42.86. The computer will print: 42.86 Multiple numbers Every format (such as ``###.##'') is a string. You can replace the format by a string variable: 10 A$="###.##" 20 PRINT USING A$; 247.91 30 PRINT USING A$; 823 40 PRINT USING A$; 7 50 PRINT USING A$; -5 60 PRINT USING A$; -80.3 The computer will print: 247.91 823.00 7.00 -5.00 -80.30 When the computer prints that column of numbers, notice that the computer prints the decimal points underneath each other so that they line up. So to make decimal points line up, say PRINT USING instead of just PRINT. To print those numbers across instead of down, say this: PRINT USING "###.##"; 247.91,823,7,-5,-80.3 It makes the computer print 247.91, then 823.00, etc., like this: 247.91823.00 7.00 -5.00-80.30 Since the computer prints those numbers so close together, they're hard to read. To make the computer insert extra space between the numbers, widen the format by putting a fourth ``#'' before the decimal point: PRINT USING "####.##"; 247.91,823,7,-5,-80.3 Then the computer will print: 247.91 823.00 7.00 -5.00 -80.30 If you say ___ PRINT USING "MY ## PALS DRANK ###.# PINTS OF GIN"; 24,983.5 the computer will print: MY 24 PALS DRANK 983.5 PINTS OF GIN Oversized numbers Suppose you say: PRINT USING "###.##"; 16238.7 The computer tries to print 16238.7 by using the format ``###.##''. But since that format allows just three digits before the decimal point, the format isn't large enough to fit 16238.7. So the computer must disobey the format. But the computer also prints a percent sign, which means, ``Warning! I am disobeying you!'' Altogether, the computer prints: %16238.70 Final semicolon At the end of the PRINT USING statement, you can put a semicolon: 10 PRINT USING "##.##"; 13.5; 20 PRINT "CREDIT" Line 10 makes the computer print 13.50. The semicolon at the end of line 10 makes the computer print CREDIT on the same line, like this: 13.50CREDIT Advanced formats Suppose you're running a high-risk business. On Monday, your business runs badly: you lose $27,931.60, so your ``profit'' is minus $27,931.60. On Tuesday, your business does slightly better than break-even: your net profit for the day is $8.95. Let's make the computer print the word PROFIT, then the amount of your profit (such as -$27,931.60 or $8.95), then the word HA (because you're cynical about how your business is going). You can do that printing in several ways. Let's explore them. . . . If you say ___ 10 A$="PROFIT######.##HA" 20 PRINT USING A$; -27931.6 30 PRINT USING A$; 8.95 the computer will print: PROFIT-27931.60HA PROFIT 8.95HA Comma If you change the format to ``PROFIT###,###.##HA'', the computer will insert a comma if the number is large: PROFIT-27,931.60HA PROFIT 8.95HA Plus sign If you change the format to ``PROFIT+#####.##HA'', the computer will print a plus sign in front of any positive number: PROFIT-27931.60HA PROFIT +8.95HA Trailing minus To print a negative number, the computer normally prints a minus sign before the number. That's called a leading minus. You can make the computer put the minus sign after the number instead; that's called a trailing minus. For example, if you change the format to ``PROFIT######.##-HA'', the computer will print a minus sign after a negative number (and no minus after a positive number), like this: PROFIT27931.60-HA PROFIT 8.95 HA Dollar sign Normally, a format begins with ##. If you begin with $$ instead (like this: ``PROFIT$$#####.##HA''), the computer will print a dollar sign before the digits: PROFIT-$27931.60HA PROFIT $8.95HA Check protection If you begin with ** (like this: ``PROFIT**#####.##HA''), the computer will print asterisks before the number: PROFIT*-27931.60HA PROFIT******8.95HA If you begin with **$ (like this: ``PROFIT**$#####.##HA''), the computer will print asterisks and a dollar sign: PROFIT*-$27931.60HA PROFIT******$8.95HA When you're printing a paycheck, use the asterisks to prevent the employee from enlarging his salary. Since the asterisks protect the check from being altered, they're called check protection. Combination You can combine several techniques into a single format. For example, you can combine the comma, the trailing minus, and the **$ (like this: ``PROFIT**$##,###.##-HA''), so that the computer will print: PROFIT**$27,931.60-HA PROFIT*******$8.95 HA E notation If you change the format to ``PROFIT##.#####^^^^HA'', the computer will print numbers by using E notation: PROFIT-2.79316E+04HA PROFIT 8.95000E+00HA